package com.caifu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.caifu.mapper.CDRecordMapper;
import com.caifu.pojo.CDRecord;
import com.caifu.service.CDRecordService;
import com.caifu.vo.IssuedVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class CDRecordServiceImpl extends ServiceImpl<CDRecordMapper, CDRecord> implements CDRecordService {

    @Resource
    private CDRecordMapper cdRecordMapper;

    @Override
    public CDRecord getCDRecord(int id) throws Exception {
        try {
            return cdRecordMapper.selectById(id);
        } catch (Exception e){
            e.printStackTrace();
            throw new Exception();
        }
    }

    @Override
    public Map editRecordList(List<IssuedVo> list) throws Exception {
        Map map = new HashMap();
        try {
            List<CDRecord> rList = new ArrayList<>();
            for (IssuedVo issuedVo : list) {
                CDRecord record = cdRecordMapper.selectById(issuedVo.getId());
                record.setIssuedState(issuedVo.getState());
                record.setIssuedTime(new Date());
                record.setContent(issuedVo.getMessage());
                rList.add(record);
            }
            rList.forEach(model -> cdRecordMapper.updateById(model));
            map.put("code", "200");
        } catch (Exception e){
            e.printStackTrace();
            map.put("code", "301");
            throw new Exception();
        }
        return map;
    }
}
